import {defineStore} from 'pinia';
import {USER_INFO_KEY} from '@/enums/cacheEnum';
import {getCache, removeCache, setCache} from '@/utils/cache';

interface UserState {
    userInfo?: object,
    // phone?: string | number;
    // headSculpture?: string,
    // nickName?: string,
    // sex?: number,
    // birthday?: string,
}

export const useUserStore = defineStore({
    id: 'user',
    state: (): UserState => ({
        userInfo: undefined
        // phone: undefined,
        // headSculpture: undefined,
        // nickName: undefined,
        // sex: undefined,
        // birthday: undefined,
    }),
    getters: {
        getUser: (state) => state.userInfo || getCache(USER_INFO_KEY),
    },
    actions: {
        setUserInfo(user: Object | undefined) {
            console.log("执行")
            console.log(user)
            const {token, ...userInfo} = user
            setCache(USER_INFO_KEY, userInfo);
            this.userInfo = userInfo;
        },
        removeUserInfo() {
            removeCache(USER_INFO_KEY);
            this.setUserInfo(undefined)
        }
    },
});
